﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExitEventApp
{
    public partial class Form1 : Form
    {
        ExitEvent ee;
        Client c;
        private DataHelper dh;
        public Form1()
        {
            InitializeComponent();
            dh = new DataHelper();
            label2.Text = "";
            label4.Text = "";
        }

        /* creates a new object ExitEvent and shows info about a client*/
        private void btnEnterId_Click(object sender, EventArgs e)
        {
            label6.Text = "Not yet";
            label8.Text = "...";
            label2.Text = "";
            label4.Text="";
            listBox1.Items.Clear();
            listBox2.Items.Clear();
            long clientId=0;
            try
            {
                 clientId = Convert.ToInt64(textBox1.Text);
                 ee = new ExitEvent("Music Festival", clientId, dh); //every time we enter a client we create the object ExitEvent
                 c = ee.getClient(clientId);
                 if (c == null)
                 {
                     MessageBox.Show("the client id does not exist");
                 }
                 else
                 {
                     List<RentedItem> l;
                     if (c != null)
                     {
                         label2.Text = c.LastName + " " + c.FirstName;
                         label4.Text = "" + c.Balance;
                         l = c.getReturnedItems();
                         if (l.Count == 0) { listBox1.Items.Add("No items"); }
                         for (int i = 0; i < l.Count; i++)
                         {
                             listBox1.Items.Add(l[i].ProductName + " ,price: " + l[i].UnitPrice + " , deposit: " + l[i].Deposit);
                         }
                         l = c.getUnreturnedItems();
                         if (l.Count == 0) { listBox2.Items.Add("No items"); }
                         for (int i = 0; i < l.Count; i++)
                         {
                             listBox2.Items.Add(l[i].ProductName + " ,price: " + l[i].UnitPrice + " , deposit: " + l[i].Deposit);
                         }
                     }
                     groupBox5.Visible = true;
                     btnExit.Visible = true;
                 }
            }
            catch (FormatException exc) { MessageBox.Show(exc.Message); }
           
        }

        /*
         * set some labels and calls the function exitEvent for the client*/
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (ee.exitEvent(c) == true)
            {
                label6.Text = "YES";
                textBox1.Text = "";
                label2.Text = "";
                label4.Text = "";
                listBox1.Items.Clear();
                listBox2.Items.Clear();
            }
            else { label6.Text = "NO"; }
            groupBox5.Visible = false;
            btnExit.Visible = false;
        }

        /*
         * check to see if we need to send balance
         * if yes, a label will be set accordingly; if no, the same*/
        private void button1_Click(object sender, EventArgs e) 
        {
            if (ee.sendBalance(c) == true)
            {
                label8.Text = "YES";
            }
            else
            {
                label8.Text="NO";
            }
        }
    }
}
